﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class School_Period : System.Web.UI.Page
{
    MyError _error = new MyError();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[Global.Session_SchoolID] == null)
        {
            Response.Redirect("~/Default.aspx");
        }

        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        PeriodDB _db = new PeriodDB();
        DataTable _dt = new DataTable("tblPeriod");
        _error = new MyError();
        _dt = _db.GetPeriodList(Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), out _error);

        gvPeriod.DataSource = _dt;
        gvPeriod.DataBind();
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        BindData();
        gvPeriod.PageIndex = e.NewPageIndex;
        gvPeriod.DataBind();
    }

    protected void AddPeriod(object sender, EventArgs e)
    {
        string PeriodID = ((TextBox)gvPeriod.FooterRow.FindControl("txtPeriodID")).Text;
        string PeriodTitle = ((TextBox)gvPeriod.FooterRow.FindControl("txtTitle")).Text;
        string ShortName = ((TextBox)gvPeriod.FooterRow.FindControl("txtShortName")).Text;
        string StartTime = ((TextBox)gvPeriod.FooterRow.FindControl("txtStartTime")).Text;
        string EndTime = ((TextBox)gvPeriod.FooterRow.FindControl("txtEndTime")).Text;
        bool IsTakeAttendence = ((CheckBox)gvPeriod.FooterRow.FindControl("chkTakeAttendence")).Checked;
        bool IsIgnoreSchedule = ((CheckBox)gvPeriod.FooterRow.FindControl("chkIgnoreSchedule")).Checked;
        bool IsActive = ((CheckBox)gvPeriod.FooterRow.FindControl("chkActive")).Checked;

        if (SavePeriod(PeriodID, PeriodTitle, ShortName, StartTime, EndTime, IsTakeAttendence, IsIgnoreSchedule, IsActive) > 0)
        {
            BindData();
        }

        //Period _item = new Period();
        //_item.PeriodID = Convert.ToInt32(PeriodID);
        //_item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
        //_item.Title = Title;
        //_item.ShortName = ShortName;


        //DateTime _dtStartTime = new DateTime(2000, 1, 1);
        //if (DateTime.TryParse(_dtStartTime.ToString("dd/MMM/yyyy") + " " + StartTime, out _dtStartTime))
        //{
        //    StartTime = _dtStartTime.Hour.ToString() + ":" + _dtStartTime.Minute.ToString();

        //    TimeSpan _starTime = new TimeSpan();
        //    if (TimeSpan.TryParse(StartTime, out _starTime))
        //    {
        //        _item.StartTime = _starTime;
        //    }
        //    else
        //    {
        //        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid start time');", true);
        //        return;
        //    }
        //}
        //else
        //{
        //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid start time');", true);
        //    return;
        //}

        //DateTime _dtEndTime = new DateTime(2000, 1, 1);
        //if (DateTime.TryParse(_dtEndTime.ToString("dd/MMM/yyyy") + " " + EndTime, out _dtEndTime))
        //{
        //    EndTime = _dtEndTime.Hour.ToString() + ":" + _dtEndTime.Minute.ToString();
        //    TimeSpan _endTime = new TimeSpan();
        //    if (TimeSpan.TryParse(EndTime, out _endTime))
        //    {
        //        _item.EndTime = _endTime;
        //    }
        //    else
        //    {
        //        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid end time');", true);
        //        return;
        //    }
        //}
        //else
        //{
        //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid end time');", true);
        //    return;
        //}

        //if (DateTime.Compare(_dtStartTime, _dtEndTime) > 0)
        //{
        //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Start time is later than end time!');", true);
        //    return;
        //}

        //_item.IsAttendence = IsTakeAttendence;
        //_item.IsIgnoreSchedule = IsIgnoreSchedule;
        //_item.IsActive = IsActive;

        //PeriodDB _db = new PeriodDB();
        //_item = _db.Save(_item);

        //if (_item.Error.ErrorNo == 0)
        //{
        //    BindData();
        //}
    }

    private int SavePeriod(string PeriodID, string PeriodTitle, string ShortName, string StartTime, string EndTime, bool IsTakeAttendence, bool IsIgnoreSchedule, bool IsActive)
    {
        Period _item = new Period();
        _item.PeriodID = Convert.ToInt32(PeriodID);
        _item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
        _item.Title = PeriodTitle;
        _item.ShortName = ShortName;

        DateTime _dtStartTime = new DateTime(1900, 1, 1);
        if (DateTime.TryParse(_dtStartTime.ToString("dd/MMM/yyyy") + " " + StartTime, out _dtStartTime))
        {
            StartTime = _dtStartTime.Hour.ToString() + ":" + _dtStartTime.Minute.ToString();

            TimeSpan _starTime = new TimeSpan();
            if (TimeSpan.TryParse(StartTime, out _starTime))
            {
                _item.StartTime = _starTime;
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid start time');", true);
                return 0;
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid start time');", true);
            return 0;
        }

        DateTime _dtEndTime = new DateTime(1900, 1, 1);
        if (DateTime.TryParse(_dtEndTime.ToString("dd/MMM/yyyy") + " " + EndTime, out _dtEndTime))
        {
            EndTime = _dtEndTime.Hour.ToString() + ":" + _dtEndTime.Minute.ToString();
            TimeSpan _endTime = new TimeSpan();
            if (TimeSpan.TryParse(EndTime, out _endTime))
            {
                _item.EndTime = _endTime;
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid end time');", true);
                return 0;
            }
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Invalid end time');", true);
            return 0;
        }

        if (DateTime.Compare(_dtStartTime, _dtEndTime) > 0)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "JSInvalidStartTime", "alert('Start time is later than end time!');", true);
            return 0;
        }

        _item.IsAttendence = IsTakeAttendence;
        _item.IsIgnoreSchedule = IsIgnoreSchedule;
        _item.IsActive = IsActive;

        PeriodDB _db = new PeriodDB();
        _item = _db.Save(_item);

        return _item.PeriodID;
    }

    protected void UpdatePeriod(object sender, GridViewUpdateEventArgs e)
    {
        string PeriodID = ((Label)gvPeriod.Rows[e.RowIndex].FindControl("lblPeriodID")).Text;
        string PeriodTitle = ((TextBox)gvPeriod.Rows[e.RowIndex].FindControl("txtTitle")).Text;
        string ShortName = ((TextBox)gvPeriod.Rows[e.RowIndex].FindControl("txtShortName")).Text;
        string StartTime = ((TextBox)gvPeriod.Rows[e.RowIndex].FindControl("txtStartTime")).Text;
        string EndTime = ((TextBox)gvPeriod.Rows[e.RowIndex].FindControl("txtEndTime")).Text;
        bool IsTakeAttendence = ((CheckBox)gvPeriod.Rows[e.RowIndex].FindControl("chkTakeAttendence")).Checked;
        bool IsIgnoreSchedule = ((CheckBox)gvPeriod.Rows[e.RowIndex].FindControl("chkIgnoreSchedule")).Checked;
        bool IsActive = ((CheckBox)gvPeriod.Rows[e.RowIndex].FindControl("chkActive")).Checked;

        if (SavePeriod(PeriodID, PeriodTitle, ShortName, StartTime, EndTime, IsTakeAttendence, IsIgnoreSchedule, IsActive) > 0)
        {
            gvPeriod.EditIndex = -1;
            BindData();
        }
    }

    protected void DeletePeriod(object sender, EventArgs e)
    {
        LinkButton lnkRemove = (LinkButton)sender;
        PeriodDB _db = new PeriodDB();
        _error = new MyError();
        _db.Delete(Convert.ToInt32(lnkRemove.CommandArgument), out _error);
        if (_error.ErrorNo == 0)
        {
            BindData();
        }
    }

    protected void EditPeriod(object sender, GridViewEditEventArgs e)
    {
        gvPeriod.EditIndex = e.NewEditIndex;
        CheckBox chkTakeAttendence = (CheckBox)gvPeriod.Rows[e.NewEditIndex].FindControl("chkTakeAttendence");
        Label lblTakeAttendence = (Label)gvPeriod.Rows[e.NewEditIndex].FindControl("lblTakeAttendence");

        if (chkTakeAttendence != null && lblTakeAttendence != null)
        {
            if (lblTakeAttendence.Text.ToUpper() == "YES")
            {
                chkTakeAttendence.Checked = true;
            }
            else
            {
                chkTakeAttendence.Checked = false;
            }
        }
        BindData();
    }

    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvPeriod.EditIndex = -1;
        BindData();
    }

    protected void gvPeriod_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv != null)
            {
                Label lblTakeAttendence = (Label)e.Row.FindControl("lblTakeAttendence");
                if (lblTakeAttendence != null)
                {
                    if (drv["IsAttendence"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsAttendence"].ToString()) == true)
                        {
                            lblTakeAttendence.Text = "Yes";
                        }
                        else
                        {
                            lblTakeAttendence.Text = "No";
                        }
                    }
                }

                Label lblIgnoreSchedule = (Label)e.Row.FindControl("lblIgnoreSchedule");
                if (lblTakeAttendence != null)
                {
                    if (drv["IsIgnoreSchedule"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsIgnoreSchedule"].ToString()) == true)
                        {
                            lblIgnoreSchedule.Text = "Yes";
                        }
                        else
                        {
                            lblIgnoreSchedule.Text = "No";
                        }
                    }
                }

                Label lblActive = (Label)e.Row.FindControl("lblActive");
                if (lblActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            lblActive.Text = "Yes";
                        }
                        else
                        {
                            lblActive.Text = "No";
                        }
                    }
                }
            }
        }
    }

    protected void gvPeriod_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)))
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            if (drv != null)
            {
                CheckBox chkTakeAttendence = (CheckBox)e.Row.FindControl("chkTakeAttendence");
                CheckBox chkIgnoreSchedule = (CheckBox)e.Row.FindControl("chkIgnoreSchedule");
                CheckBox chkActive = (CheckBox)e.Row.FindControl("chkActive");

                if (chkTakeAttendence != null)
                {
                    if (drv["IsAttendence"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsAttendence"].ToString()) == true)
                        {
                            chkTakeAttendence.Checked = true;
                        }
                        else
                        {
                            chkTakeAttendence.Checked = false;
                        }
                    }
                }


                if (chkIgnoreSchedule != null)
                {
                    if (drv["IsIgnoreSchedule"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsIgnoreSchedule"].ToString()) == true)
                        {
                            chkIgnoreSchedule.Checked = true;
                        }
                        else
                        {
                            chkIgnoreSchedule.Checked = false;
                        }
                    }
                }


                if (chkActive != null)
                {
                    if (drv["IsActive"] != null)
                    {
                        if (Convert.ToBoolean(drv["IsActive"].ToString()) == true)
                        {
                            chkActive.Checked = true;
                        }
                        else
                        {
                            chkActive.Checked = false;
                        }
                    }
                }
            }
        }
    }
}